#!/usr/bin/perl

use warnings;
use strict;
use Net::SMTP::SSL;
use Encode;
use utf8;

#$flag = utf8::is_utf8(STRING);

#binmode STDOUT,":utf8";

#&smtp_mailto('wujiming@ixiqing.cn','test subject','test_message测试');
#print "send mail over\n";

sub smtp_mailto
{
my ($mail_tos, $mail_subject, $mail_content) = @_; 
my ($sendAccount,$sendAccountPw);
my ($username,$domain,$mail_to,$mail_cc,$mail_bcc,$smtp,$auth_return);
my $pair;

my @pairs = split(/\,/, $mail_tos);
	foreach $pair (@pairs)
	{
		$mail_to = $pair;
		$mail_to =~ tr/A-Z/a-z/;
		($username,$domain) = split(/\@/, $mail_to);

		$sendAccount = 'demo@163.com';
		$sendAccountPw = '123';
		

		#$mail_cc 抄送 $mail_bcc 暗送
		$smtp = Net::SMTP::SSL->new(
		'smtp.exmail.qq.com',
		Hello=>'HELLO',
		Port=>465,
		LocalPort=>0,
		Debug=>1);
		die("smtp undefined: $@") if !defined $smtp;
		 
		$auth_return = $smtp->auth($sendAccount,$sendAccountPw);
		die("auth error: $@") if !defined $auth_return;
		 
		$smtp->mail($sendAccount);
		$smtp->to($mail_to);
		if ($mail_cc) {
		   $smtp->cc($mail_cc);
		}
		if ($mail_bcc) {
		   $smtp->bcc($mail_bcc);
		}
		$smtp->data();
		$smtp->datasend("From: $sendAccount\n");
		$smtp->datasend("To: $mail_to\n");
		$smtp->datasend("Content-Type: text/html;charset=utf-8 \n");
		$smtp->datasend("Subject: $mail_subject\n");
		$smtp->datasend("\n");
		$smtp->datasend("$mail_content");
		$smtp->datasend("\n");
		$smtp->dataend();
		$smtp->quit;
	}
}






# mail_user should be your_mail@163.com
sub send_mail{
  my ($to_address,$subject,$message) = @_;
#  my $to_address  = 'want_to@gmail.com';
  my $mail_user = '11680001@qq.com';
  my $mail_pwd    = 'khszklsoxcnfbjed';
  my $mail_server = 'smtp.qq.com';
  my $from    = "From: $mail_user\n";
#  my $subject = "Subject: here comes the subject\n";


#  my $message = <<CONTENT; 
#                **********************
#                here comes the content
#                **********************
#CONTENT


   my $smtp = Net::SMTP->new($mail_server,
                             Timeout =>120,
                             Debug   =>1,
                             Port => 465,
                             SSL =>1,) or die "ERROR! $!";
   $smtp->auth('LOGIN', $mail_user, $mail_pwd) || die "Auth Error! $!";
   $smtp->mail($mail_user);
   $smtp->to($to_address);
   $smtp->data();             # begin the data
   $smtp->datasend($from);    # set user
   $smtp->datasend($subject); # set subject
   $smtp->datasend($message); # set content
   $smtp->dataend();
   $smtp->quit();
}


1;
